//
// Created by AnTler on 2018/11/29.
//

#include "TouchUtils.h"
#include "LuaJavaHelper.h"

#include "JNIDef.h"

namespace TouchUtils {
    void down(int index, int x, int y) {
        JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
        if (!env) {
            return;
        }

        LuaJavaHelper::callStaticVoidMethod(env, CLASS_NAME_TOUCHUTILS, METHOD_NAME_TOUCHUTILS_DOWN,
                                            METHOD_SIG_TOUCHUTILS_DOWN, index, x, y);
    }

    void move(int index, int x, int y) {
        JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
        if (!env) {
            return;
        }

        LuaJavaHelper::callStaticVoidMethod(env, CLASS_NAME_TOUCHUTILS, METHOD_NAME_TOUCHUTILS_MOVE,
                                            METHOD_SIG_TOUCHUTILS_MOVE, index, x, y);
    }

    void up(int index, int x, int y) {
        JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
        if (!env) {
            return;
        }

        LuaJavaHelper::callStaticVoidMethod(env, CLASS_NAME_TOUCHUTILS, METHOD_NAME_TOUCHUTILS_UP,
                                            METHOD_SIG_TOUCHUTILS_UP, index, x, y);
    }

    void tap(int x, int y) {
        JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
        if (!env) {
            return;
        }

        LuaJavaHelper::callStaticVoidMethod(env, CLASS_NAME_TOUCHUTILS, METHOD_NAME_TOUCHUTILS_TAP,
                                            METHOD_SIG_TOUCHUTILS_TAP, x, y);
    }

    void swipe(int x1, int y1, int x2, int y2, int duration) {
        JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
        if (!env) {
            return;
        }

        LuaJavaHelper::callStaticVoidMethod(env, CLASS_NAME_TOUCHUTILS,
                                            METHOD_NAME_TOUCHUTILS_SWIPE,
                                            METHOD_SIG_TOUCHUTILS_SWIPE, x1, y1, x2, y2, duration);
    }

    void press(int keyCode, bool longPress) {
        JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
        if (!env) {
            return;
        }

        LuaJavaHelper::callStaticVoidMethod(env, CLASS_NAME_TOUCHUTILS,
                                            METHOD_NAME_TOUCHUTILS_PRESS,
                                            METHOD_SIG_TOUCHUTILS_PRESS, keyCode,
                                            longPress ? JNI_TRUE : JNI_FALSE);
    }

    void press(const char *keyName, bool longPress) {
        JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
        if (!env) {
            return;
        }

        ScopedLocalRef<jstring> jkeyName(env, env->NewStringUTF(keyName));
        if (jkeyName) {
            LuaJavaHelper::callStaticVoidMethod(env, CLASS_NAME_TOUCHUTILS,
                                                METHOD_NAME_TOUCHUTILS_PRESS,
                                                METHOD_SIG_TOUCHUTILS_PRESS_KEYNAME,
                                                (jstring) jkeyName,
                                                longPress ? JNI_TRUE : JNI_FALSE);
        }
    }

    void doublePress(int keyCode) {
        JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
        if (!env) {
            return;
        }

        LuaJavaHelper::callStaticVoidMethod(env, CLASS_NAME_TOUCHUTILS,
                                            METHOD_NAME_TOUCHUTILS_DOUBLEPRESS,
                                            METHOD_SIG_TOUCHUTILS_DOUBLEPRESS, keyCode);
    }

    void doublePress(const char *keyName) {
        JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
        if (!env) {
            return;
        }

        ScopedLocalRef<jstring> jkeyName(env, env->NewStringUTF(keyName));
        if (jkeyName) {
            LuaJavaHelper::callStaticVoidMethod(env, CLASS_NAME_TOUCHUTILS,
                                                METHOD_NAME_TOUCHUTILS_DOUBLEPRESS,
                                                METHOD_SIG_TOUCHUTILS_DOUBLEPRESS_KEYNAME,
                                                (jstring) jkeyName);
        }
    }
};